package kaaengine.loaders
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import kaaengine.core.isometric.IsoStructure;
	import kaaengine.display.BitmapDataRenderer;
	import kaaengine.display.isometric.IsoBlock;
	
	import mx.collections.ArrayCollection;
	[Event(name="STRUCTURES_LOADED", type="flash.events.Event")]
	public class StructuresLoader extends EventDispatcher
	{
		private static var DELIMITER:String = "\n";
		
		public var renderablesRepository:ArrayCollection;
		public var isoBlockRepository= new ArrayCollection();
		public var structuresRepository= new ArrayCollection();
		private var imagesAlreadyLoaded:Boolean;
		private var imagesDirectory:String;
		
		private var tab:Array;
		
		public function StructuresLoader(_imagesDirectory:String)
		{
			imagesDirectory = _imagesDirectory;
		}
		
		public function loadStructures(_imagesAlreadyLoaded:Boolean, fileName:String):void {
			imagesAlreadyLoaded = _imagesAlreadyLoaded;
			var request:URLRequest = new URLRequest(fileName);
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onStructureFileLoaded);	
			loader.load(request);			
		}
		public function onStructureFileLoaded(event:Event):void {	
			var loader:URLLoader = URLLoader(event.target);
			var str:String = loader.data as String;
			tab = str.split(DELIMITER); 
			for(var i:int = 0 ; i < tab.length ;i++ ) {
				trace(tab[i]);
			}
			var currentZone:String;
			
			if( !imagesAlreadyLoaded ) {
				renderablesRepository = new ArrayCollection();
				var imagesNamesTab:Array = new Array();
				for(var i:int=0;i< tab.length;i++){
				var line:String = tab[i];
				
					if(line.substr(0,1)=="#") {
						currentZone = line;
					} else {	
						if( currentZone == "#images"){							
							imagesNamesTab.push(line);
						}else {
							break;
						}
					}
				}
				var il:ImageLoader = new ImageLoader();
				il.addEventListener("images_loaded",imagesLoaded);
				il.renderables = renderablesRepository;
				il.loadImagesFromArray(imagesDirectory,imagesNamesTab);		
				
			} 
			
		}
		public function imagesLoaded(event:Event):void {
			trace("all images loaded");
			finish();
		}	
		public function finish():void {
			var currentZone:String;
			for(var i:int=0;i< tab.length;i++){
				var line:String = tab[i];
				
				if(line.substr(0,1)=="#") {
					currentZone = line;
				} else {	
					 if( currentZone == "#blocks"){
						var ss:Array = line.split(";");
						var ib:IsoBlock = new IsoBlock();							
						if( ss.length == 1 ) {
							ib.bitmaps[0] = renderablesRepository.getItemAt(Number(ss[0])) as BitmapDataRenderer;
							ib.bitmaps[1] = ib.bitmaps[0];
							ib.bitmaps[2] = ib.bitmaps[0];
							ib.bitmaps[3] = ib.bitmaps[0];
						} else {
							ib.bitmaps[0] = renderablesRepository.getItemAt(Number(ss[0])) as BitmapDataRenderer;
							ib.bitmaps[1] = renderablesRepository.getItemAt(Number(ss[1])) as BitmapDataRenderer;
							ib.bitmaps[2] = renderablesRepository.getItemAt(Number(ss[2])) as BitmapDataRenderer;
							ib.bitmaps[3] = renderablesRepository.getItemAt(Number(ss[3])) as BitmapDataRenderer;
						}
						isoBlockRepository.addItem(ib);							
					}else if( currentZone == "#structures"){							
						var ss:Array = line.split(";");							
						var structure:IsoStructure = new IsoStructure();
						structure.blocks =  new Vector.<IsoBlock>(ss.length);
						for(var sIndex:int=0;sIndex<ss.length;sIndex++){
							structure.blocks[sIndex] = isoBlockRepository.getItemAt(Number(ss[sIndex])) as IsoBlock;
						}
						structuresRepository.addItem(structure);
					}	
				}
			}
			trace("structures loaded");
			dispatchEvent(new Event("STRUCTURES_LOADED"));		
		}

	}
}